<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
   exit;
}
//ɲ룬£ɾȲˢ
if($continue!="")header("localtion: p_type.php"); 

$menu_index = 1;
$page_title = 'TABLA';
$guide_message = 'Product | Type';
$out_ary = array();
$ppp = 20;
$page_show = '';

//echo "<pre>";print_r($_POST);
getMyParam('action');

//echo $search_key;
//if($action!="")exit;

$search_flag = false;
$page_url = 'p_type.php?';
$sql_where = '';


if ($action != "") {

	//SAVE
	if($action=='order'){
	getMyParam('orders');
	getMyParam('order_id'); 

	for($i=0;$i<count($orders);$i++)
	{
       $sql = "update `pro_type` set orders='{$orders[$i]}' where id={$order_id[$i]} ";
       $db->query($sql);
	}
	header("LOCATION:p_type.php?continue=true");
	}

    if ($action == 'add' || $action == 'edit') {
        getMyParam('name'); 
		 getMyParam('name_danish'); 
        getMyParam('orders');
		//$orders=8;
        if ($action == 'add') {
		//orders
		$sql="select max(orders)+1 as max from pro_type";
		$order=$db->get_one($sql);
		$orders=$order['max'];
		
            if ($name != "") {
                // Ĳ
                $sql = "INSERT INTO `pro_type` (`name`,`name_danish`, `orders`)
						VALUES 
						('$name','$name_danish', '$orders')";
                $db->query($sql);
                //$category_id = $db->insert_id();
				$msg="This Type has been added.";
	            $href="p_type.php";
				include_once('menu_list.php');
				require_once PrintEot('a_header');
				require_once PrintEot('a_msg');
				require_once PrintEot('a_footer');   
				exit;
            }
        } elseif ($action == 'edit') {
            getMyParam('cid');

            $sql = "UPDATE `pro_type` SET
                   `orders` = '$orders', 
				   `name` =  '$name' ,
                   `name_danish` =  '$name_danish' 
                   WHERE `id` = '$cid'";
            $db->query($sql);
			$msg="This Type has been updated.";
			$href="p_type.php";
			include_once('menu_list.php');
			require_once PrintEot('a_header');
			require_once PrintEot('a_msg');
			require_once PrintEot('a_footer');   
			exit;
        }

    }

    if ($action == 'del') {
        getMyParam('cid');
        if (is_numeric($cid) && $cid > 0) {     
            
            $sql_1 = "DELETE FROM `pro_type` WHERE `id` = '" . $cid . "'";
            $db->query($sql_1);
            
            // ɾsizeжӦsize options
            //$sql_2 = "DELETE FROM `size` WHERE `category_id` = '" . $cid . "'";
           // $db->query($sql_2);
 
 			$msg="This Type has been deleted. ";
			$href="p_type.php";
			include_once('menu_list.php');
			require_once PrintEot('a_header');
			require_once PrintEot('a_msg');
			require_once PrintEot('a_footer');   
			exit;
         }
     }
	
	if ($action == "search") {
	getMyParam('search_key');
    $sql_where .= " where  `name` LIKE '%$search_key%' or `id` LIKE '%$search_key%'";
    $page_url = 'p_type.php?action=search&search_key=' . $search_key;
	}
	
}else $sql_where="";



$sql = "SELECT * FROM `pro_type` $sql_where";

$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);



// *** page code start ***
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}
// *** page code end ***
$sql = "SELECT `id`, `name`, `orders` FROM `pro_type` $sql_where ORDER BY `orders` ASC, `id` DESC LIMIT $start_limit, $ppp";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($action == "search" && trim($search_key) != "") {
        $rs['name'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['name']);
		$rs['id'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['id']);
    }
    $out_ary[] = $rs;
}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);
//echo $page_show;
//echo "<pre>";print_r($out_ary);
$debug = 1;
debuginfo();

include_once('menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_p_type');
require_once PrintEot('a_footer');

?>